const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        auth_value:1,
        up_img:'up_person.png',
        agree:0,
        auth_type:[
            {value: 1, name: '车主',checked:1},
            {value: 2, name: '企业',checked:0},
          
        ],
        card_type:[
            {value: 1, name: '身份证'},
            {value: 2, name: '营业执照'},
        ],
        auth_value:1,
        upload:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that=this;
        
        wx.showShareMenu({
            menus: ['shareAppMessage', 'shareTimeline'],
            success(res) {
              console.log(res)
            },
            fail(e) {
              console.log(e)
            }
          })             
          var deviceInfo = wx.getDeviceInfo();
          console.log(deviceInfo)
          let d = app.globalData;
          var open=wx.getStorageSync('open');  
          this.setData({
              //id:id,
              open:open,
              width:d.windowWidth,
              height:d.windowHeight,
              statusBarHeight:d.statusBarHeight,
              navHeight:d.navHeight,
              marginTop:d.navTitleTop,
              bottom:d.bottom,
              domain:d.domain,
              imgUrl:d.imgUrl,
              platform:deviceInfo.platform
          })
    },
    radioChange(e){
        console.log(e);
        var auth_type = this.data.auth_type;
        var value = e.detail.value;
    
        auth_type.forEach(function(val,index){
            if(val.value == value){
               val.checked = 1;
            }else{
                val.checked = 0;
            }
        })
        this.setData({
            auth_type:auth_type,
            auth_value:value
        })
    },
    agreeChange(){
        var agree = this.data.agree;
        if(agree==1){
            agree=0;
        }else{
            agree = 1 ;
        }
        this.setData({
            agree:agree
        })
    },
    goBack(){
        wx.navigateBack();
      },
      goto(e){
        console.log(e);
        var url=e.currentTarget.dataset.url;
        console.log(url);
        var index=e.currentTarget.dataset.index;
        app.goto(url,index)
      // console.log(url);
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
      let that=this;
    
      var open=wx.getStorageSync('open');
        if(open.user_id==0){

            app.showModal('请先绑定手机号','bind','#')
            return ;
        }
      console.log(open);
      var auth = open.auth;     
        wx.request({
            url: app.globalData.domain+'/wechat/auth/getIndex',
            data:{user_id:open.user_id,open_id:open.id,from:1},
            success:function(res){
              console.log(res);          
                var data=res.data;
                console.log(data)
                  that.setData({
                    partner:data.partner
                   })
              
        
            }
          })
         
     
    },
    goBack(){
        wx.navigateBack();
      },
    goto:function(e){
        console.log(e);
        var url = e.currentTarget.dataset.url;
        var index = e.currentTarget.dataset.index;
        app.goto(url,index)
    },
    uploadCard:function(e){ 
        let that=this;
        //let imageType='idcard';
        let d = e.currentTarget.dataset;
        let imageType = d.auth_value;
        let direction = d.direction;
        var auth_value = this.data.auth_value;
        if(auth_value==1){
            imageType = 'idcard';
        }else{
            imageType= 'company';
        }
       
        if(this.data.partner.status == 1){
          wx.showToast({
            title: '已通过不能修改',
            icon:"error"
          })
          return false;
        }
        wx.chooseMedia({
          count: 1,
          mediaType: ['image'],
          sizeType:["compressed"],
          sourceType:["camera","album"],
          success(res){
              console.log(res);
            let tempFilePaths=res.tempFiles[0].tempFilePath;
            that.upload(imageType,direction,tempFilePaths)
          }
        })
        
      },
      upload:function(imageType,direction,path){
        wx.showToast({
          icon:"loading",
          title: '正在上传',
        })
        let that = this;        
      var timestamp=new Date().getTime(); 
      var partner = this.data.partner;
      var domain = this.data.domain;
      console.log(path)

        wx.uploadFile({
         filePath:path ,
          name: 'file',
          url: app.globalData.domain+'/wechat/auth/upload',
          formData:{
            imageType:imageType,
            direction:direction
            },
            header: { 
              'content-type':'application/x-www-form-urlencoded'       
             },       
          success(res){
            console.log(res);
            var data=JSON.parse(res.data);
           console.log(data);
            if(data.error==0){  
                that.setData({
                    upload:1
                })            
              console.log(direction)
              if(imageType == 'idcard'){
              
                    var word=data.info.words_result;
                
                    console.log(word);
                    var idcard = word.公民身份号码;
                    var name =  word.姓名; 
                    console.log(name);
                    var username=name.words;
                    var idcards=idcard.words;
                    console.log(username);
                    console.log(idcards);
                    partner.idcard_front = data.path;
                    that.setData({
                        idcard_front:data.path,
                        name:username,
                        idcard:idcards,
                        partner:partner
                    })  
                 
                  
             
              }
              if(imageType == 'company'){
              
                var word=data.info.words_result;
            
                console.log(word);
                var idcard = word.社会信用代码;
                var name =  word.单位名称; 
                console.log(name);
                var username=name.words;
                var idcards=idcard.words;
                console.log(username);
                console.log(idcards);
                partner.idcard_front = data.path;
                that.setData({
                    idcard_front:data.path,
                    name:username,
                    idcard:idcards,
                    partner:partner
                })  
             
              
         
          }
       
            }
          }
      
        })
      },
      cardAuth:function(){
        let that = this; 
        let data=this.data;
        if(data.upload == 0 ){
            app.showModal('等待证件识别结束');
            return ;
        }
        if(data.agree == 0 ){
            app.showModal('请阅读并勾中用户协议隐私政策');
            return ;
        }
        console.log('cardAuth');
        wx.request({
            url: app.globalData.domain+'/wechat/auth/auth',
            data:{idcard_front:data.idcard_front,idcard:data.idcard,name:data.name,user_id:data.open.user_id,open_id:data.open.id,auth_type:data.auth_value},
            success(res){
                console.log(res);
                var data=res.data;
                if(data.code==200){
                 //   wx.setStorageSync('open', data.user);
                    that.setData({
                        partner:data.partner
                    })
                    wx.showToast({
                      title: data.msg,
                    })
                }
            }
        }) 
      },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})